package lib.bone.nepudata.service.impl;

import cn.hutool.core.lang.Validator;
import lib.bone.access.constant.ResearchTypeConst;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.PageResultDTO;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.common.exception.BusinessException;
import lib.bone.nepudata.dao.NepuThinkTankDAO;
import lib.bone.nepudata.domain.entity.NepuThinkTank;
import lib.bone.nepudata.expose.WorkloadService;
import lib.bone.nepudata.service.ResearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author :贾斯淇
 */

/**
 * cg 2022-4-6
 */

@Service
public class NepuThinkTankServiceImpl implements ResearchService<NepuThinkTank,String> {
    @Autowired
    NepuThinkTankDAO nepuThinkTankDAO;

    @Autowired(required = false)
    WorkloadService workloadService;

    @Override
    public String getProjectCode(String s) {
        return "无";
    }


    public Optional<NepuThinkTank> findByNameAndLeader(String name,String leader){
        return nepuThinkTankDAO.findByNameAndLeader(name,leader);
    }

    @Override
    public List<NepuThinkTank> getByIdIn(List<String> ids) {
        return nepuThinkTankDAO.findByIdIn(ids);
    }

    @Override
    public String getServiceType() {
        return ResearchTypeConst.THINKTANK;
    }

    @Override
    public NepuThinkTank saveOne(NepuThinkTank nepuThinkTank) {
        if (Validator.isNotEmpty(nepuThinkTank.getId())){
            throw new BusinessException(ResponseCodeConst.ERROR_PARAM);
        }
        return nepuThinkTankDAO.save(nepuThinkTank);    }

    @Override
    public NepuThinkTank updateOne(NepuThinkTank nepuThinkTank) {
        if (Validator.isEmpty(nepuThinkTank.getId())){
            throw new BusinessException(ResponseCodeConst.ERROR_PARAM);
        }
        return nepuThinkTankDAO.save(nepuThinkTank);
    }

    //@TODO
    @Override
    public PageResultDTO<NepuThinkTank> getAll(Pageable pageable, Example<NepuThinkTank> example) {
        Page<NepuThinkTank> page;
        //是否为空 --
        if(Validator.isEmpty(example)){
            page = nepuThinkTankDAO.findAll(pageable);
        }else {
            page = nepuThinkTankDAO.findAll(example,pageable);
        }
        List<NepuThinkTank> list = page.getContent();
        PageResultDTO<NepuThinkTank> pageResultDTO = new PageResultDTO<>();
        pageResultDTO.setContent(list);
        pageResultDTO.setSize(page.getSize());
        pageResultDTO.setTotalPages(page.getTotalPages());
        pageResultDTO.setNumber(page.getNumber());
        pageResultDTO.setTotalElements(page.getTotalElements());

        return pageResultDTO;
    }

    @Override
    public ResponseDTO deleteById(String id) {
        Integer useCount = workloadService.countByLinkId(id);
        // Long useCount = nepuResearchWorkloadDAO.countByResearchTypeAndMapperId("教学科研获奖",id);
        if(useCount > 0){
            return ResponseDTO.wrap(ResponseCodeConst.FORBIDDEN,"存在关联该奖励的科研工作量信息,拒绝执行删除!");
        }
        //判断数据是否存在
        Optional<NepuThinkTank> nepuThinkTankOp = nepuThinkTankDAO.findById(id);
        if(!nepuThinkTankOp.isPresent()) return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND,"ID:["+id+"]对应数据不存在");
        nepuThinkTankDAO.deleteById(id);
        return ResponseDTO.succMsg("删除成功!");
    }

    @Override
    public Optional<NepuThinkTank> getById(String id) {
        Optional<NepuThinkTank> nepuThinkTankOp = nepuThinkTankDAO.findById(id);
        return nepuThinkTankOp;
    }

//    @Override
//    public ResponseDTO<NepuThinkTank> saveOne(NepuThinkTankAddDTO nepuThinkTankAddDTO) {
//        NepuThinkTank nepuThinkTank = new NepuThinkTank();
//        String dictId = nepuThinkTankAddDTO.getThinktankDictId();
//        BeanUtil.copyProperties(nepuThinkTankAddDTO,nepuThinkTank, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
//        Optional<NepuThinkTankDict> dictOp = dictService.getById(dictId);
//        if (!dictOp.isPresent()){
//            return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND,"词典ID["+dictId+"]对应资源不存在");
//        }
//        NepuThinkTankDict dict = dictOp.get();
//        //初始化赋值为最大值
//        nepuThinkTank.setWorkload(dict.getWorkloadMax());
//        /** 查询是否已经存在对应数据 **/
////        Example<NepuThinkTank> example = Example.of(nepuThinkTank);
////        List<NepuThinkTank> nepuThinkTankList = nepuThinkTankDAO.findAll(example);
////        if(nepuThinkTankList.size() > 0){
////            return ResponseDTO.wrap(ResponseCodeConst.FORBIDDEN,"相同记录已存在,拒绝新建请求");
////        }
//        return ResponseDTO.succData(nepuThinkTankDAO.save(nepuThinkTank));
//    }
//
//    @Override
//    public ResponseDTO<NepuThinkTank> updateOne(NepuThinkTankUpdateDTO nepuThinkTankUpdateDTO) {
//        //@TODO 如果在审核流程中 应拒绝修改，或者修改后退回初始流程位置
//        Optional<NepuThinkTank> nepuThinkTankOp = nepuThinkTankDAO.findById(nepuThinkTankUpdateDTO.getId());
//        if(!nepuThinkTankOp.isPresent()) return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND,"ID:["+
//                nepuThinkTankUpdateDTO.getId()+"]对应的智库类成果不存在");
//        NepuThinkTank nepuThinkTank = nepuThinkTankOp.get();
//        BeanUtil.copyProperties(nepuThinkTankUpdateDTO,nepuThinkTank,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
//        return ResponseDTO.succData(nepuThinkTankDAO.save(nepuThinkTank));
//    }
}
